package com.example.whatrubbish.controller;

import com.example.whatrubbish.model.Order;
import com.example.whatrubbish.service.OrderService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.DemoException;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.OrderDetailVO;
import com.example.whatrubbish.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @GetMapping("")
    public JsonResult get(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "userId") String userId,
                          @RequestParam(value = "status") String status,
                          @RequestParam(value = "title") String title) {
        PageBean<OrderDetailVO> pageBean = orderService.selectByCondition(pageIndex, pageSize, userId, status, title);
        return new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
    }

    @PostMapping("")
    public JsonResult insert(@RequestBody @Validated Order record) throws DemoException {
        orderService.insert(record);
        return new JsonResult(Constants.STATUS_SUCCESS, "success");
    }

    @PutMapping("")
    public JsonResult update(@RequestBody Order record) throws DemoException {
        orderService.update(record);
        return new JsonResult(Constants.STATUS_SUCCESS, "success");
    }

    @DeleteMapping("")
    public JsonResult delete(@RequestParam("id") Long id) {
        orderService.delete(id);
        return new JsonResult(Constants.STATUS_SUCCESS, "success");
    }

    @GetMapping("/cancel")
    public JsonResult cancel(@RequestParam("id") Long id) {
        orderService.cancel(id);
        return new JsonResult(Constants.STATUS_SUCCESS, "success");
    }
}
